﻿using The_World_Server;
using Lidgren.Network;
using Server.Manager;
using TheWorldLibrary.Characters;
using TheWorldLibrary.Sprites;
using TheWorldLibrary.Enumeration;
using Server.PC;
using Microsoft.Xna.Framework;

namespace Server.Network
{
    class HandleTCP
    {
        // private constructor
        private HandleTCP() { }

        // singleton accessor
        static HandleTCP instance;
        public static HandleTCP Instance
        {
            get
            {
                if (instance == null)
                    instance = new HandleTCP();
                return instance;
            }
        }

        public void HandleData(int index, NetIncomingMessage data)
        {
            // packet header
            int msgType = data.ReadInt32();

            // check packet number
            if (msgType < 0 || msgType >= (int)ClientPackets.CMSG_COUNT - 1)
            {
                Program.server.TextAdd("Packet error");
                return;
            }

            switch (msgType)
            {
                case (int)ClientPackets.CLogin: HandleLogin(index, data); break;
                case (int)ClientPackets.CAddAccount: HandleAddAccount(index, data); break;
                case (int)ClientPackets.CAddChar: HandleAddCharacter(index, data); break;
                case (int)ClientPackets.CUseChar: HandleUseChar(index, data); break;
                case (int)ClientPackets.CSayMsg: HandleSayMsg(index, data); break;                
                case (int)ClientPackets.CPlayerMove: HandlePlayerMovement(index, data); break;
            }
        }


        #region Account/Character/Registration

        private void HandleLogin(int index, NetIncomingMessage data)
        {
            string accountName = data.ReadString();
            string password = data.ReadString();

            DataManager.Instance.AccountLogin(accountName, password, index);
        }

        private void HandleAddAccount(int index, NetIncomingMessage data)
        {
            string login = data.ReadString();
            string password = data.ReadString();

            DataManager.Instance.CreateAccount(login, password, index);
        }

        private void HandleAddCharacter(int index, NetIncomingMessage data)
        {
            string accountName = data.ReadString();
            string entityName = data.ReadString();
            int spriteNum = data.ReadInt32();
            EntityType etype = (EntityType)data.ReadInt32();
            EntityGender gender = (EntityGender)data.ReadInt32();

            DataManager.Instance.CreateCharacter(accountName,entityName, spriteNum, etype, gender, index);

        }

        private void HandleUseChar(int index, NetIncomingMessage data)
        {
            string name = data.ReadString();
            DataManager.Instance.LoadPlayer(name, index);
        }

        #endregion

        private void HandleSayMsg(int index, NetIncomingMessage data)
        {
            // read packet
            string msg = data.ReadString();

            // TODO: Add name to message
            ServerTcp.Instance.SendMapMsg(index, msg);
        }

        private void HandlePlayerMovement(int index, NetIncomingMessage data)
        {
            int dir;
            float x, y;
            dir = data.ReadInt32();
            x = data.ReadFloat();
            y = data.ReadFloat();

            Player.Instance.Players[index].Entity.EntityDir = (AnimationKey)dir;
            Player.Instance.Players[index].Sprite.Position = new Vector2(x, y);

            ServerTcp.Instance.SendPlayerMovement(index);
        }
    }
}
